var models = require('../../db/models')
var server = require('../server')
var db = require('../../db/connect')
var encrypt = require('../../libs/encrypt')
var jsonres = require('../../libs/jsonres')
var mongoose = require('mongoose')

server.get('/api/accounts',function(req,res) {
    var m = req.session.u
    db.find('accounts',{mobile: m, deleted: false},function(err,result) {
        if (!!!err) {
            console.log(result)
            res.send(jsonres(200,'success',result))
        } else {
            res.send(jsonres(-1,'查询失败',err))
        }
    })
})

server.post('/api/accounts',function(req,res) {
    var JSONBody = req.body
    var u = JSONBody.u
    var p = JSONBody.p
    var m = req.session.u
    var platform = JSONBody.platform
    var avatar_urls = {
        'KK扑克': 'http://is1.mzstatic.com/image/thumb/Purple122/v4/da/b6/01/dab6015f-fa36-fd3e-ba5c-832ba5bccaa1/source/175x175bb.jpg',
        '德扑圈': 'http://is3.mzstatic.com/image/thumb/Purple122/v4/e3/e6/11/e3e61108-41a9-110f-e468-5205dc1a9d8b/source/175x175bb.jpg'
    }
    db.insert('accounts',{mobile: m, user: u, pwd: p, platform: platform, status: false, avatar: avatar_urls[platform],deleted: false},function(err) {
        if (!!!err) {
            res.send(jsonres(200,'success',null))
        } else {
            res.send(jsonres(-1,'插入失败',err))
        }
    })
})

server.post('/api/delaccount',function(req,res) {
    var JSONBody = req.body
    var id = JSONBody.id
    db.update('accounts',{_id: new mongoose.Types.ObjectId(id)},{deleted: true},false,function(err) {
        if (!!!err) {
            res.send(jsonres(200,'success',null))
        } else {
            res.send(jsonres(-1,'删除失败',err))
        }
    })
})
